{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b6b4b4ee",
   "metadata": {},
   "source": [
    "此题在3.0实验手册223"
   ]
  },
  {
   "cell_type": "raw",
   "id": "acb18c05",
   "metadata": {},
   "source": [
    "目的是为了预测未来某个用户对某个书籍的评分是多少？\n",
    "解决办法：\n",
    "    1.看用户和哪些用户相似，这些相似的用户对这本书的评分是多少。然后利用算法(求平均)计算用户对这本书的评分是多少\n",
    "        张三     水浒传       评分？\n",
    "        张三和哪些用户相似：李四(8)     王五(9)     赵六(8)     孙七(9)      刘八(10)\n",
    "        张三对水浒传的评分为所有相似用户对水浒传评分的均值：评分为9分\n",
    "    2.看和需要评分的书籍相似的书，用户对这些相似的书的评分为多少，然后再计算用户对这本书的评分是多少\n",
    "        张三     水浒传       评分？\n",
    "        水浒传和哪些书籍相似：三国演义(0)     西游记(0)     红楼梦(0)     射雕英雄传(0)\n",
    "        张三对水浒传的评分为张三对所有相似书籍评分的均值：评分为0，这里为0就说明预测无意义，也说明张三没有对上面的书籍评分过\n",
    "张三：蜡笔小新     骆驼祥子    啊Q正传\n",
    "预测：张三对水浒传的评分\n",
    "1.书籍之间的相似性\n",
    "2.看预测的书籍和张三看过的哪些书籍相似\n",
    "3.在获取张三对相似书籍的评分\n",
    "4.按照自定的评分规则对书进行评分"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "90c57824",
   "metadata": {},
   "source": [
    "1.读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "cd18ba47",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "# 导入sklearn库中的cosine_similarity函数\n",
    "from sklearn.metrics.pairwise import cosine_similarity\n",
    "# 读取数据集，并选择 User-ID,ISBN,Book-Rating 三列\n",
    "df = pd.read_csv('./data/book/BX-Book-Ratings.csv',sep=';',encoding='latin-1',usecols=['User-ID','ISBN','Book-Rating'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f132cb61",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>User-ID</th>\n",
       "      <th>ISBN</th>\n",
       "      <th>Book-Rating</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>276725</td>\n",
       "      <td>034545104X</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>276726</td>\n",
       "      <td>0155061224</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>276727</td>\n",
       "      <td>0446520802</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>276729</td>\n",
       "      <td>052165615X</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>276729</td>\n",
       "      <td>0521795028</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   User-ID        ISBN  Book-Rating\n",
       "0   276725  034545104X            0\n",
       "1   276726  0155061224            5\n",
       "2   276727  0446520802            0\n",
       "3   276729  052165615X            3\n",
       "4   276729  0521795028            6"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3d598686",
   "metadata": {},
   "source": [
    "2.数据预处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3f6329cc",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 构建评分矩阵，将缺失值填充为0\n",
    "rating_matrix = df.pivot_table(index='User-ID',columns='ISBN',values='Book-Rating')   #把ISBN这列的值当作矩阵的列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "747a5280",
   "metadata": {},
   "outputs": [],
   "source": [
    "rating_matrix.fillna(0,inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7cca5bd3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>ISBN</th>\n",
       "      <th>0002231115</th>\n",
       "      <th>0002232766</th>\n",
       "      <th>0002240114</th>\n",
       "      <th>000225669X</th>\n",
       "      <th>000254794</th>\n",
       "      <th>0006128831</th>\n",
       "      <th>0006379702</th>\n",
       "      <th>0006485294</th>\n",
       "      <th>000651118X</th>\n",
       "      <th>0006511929</th>\n",
       "      <th>...</th>\n",
       "      <th>9727474799</th>\n",
       "      <th>9728608225</th>\n",
       "      <th>9728609187</th>\n",
       "      <th>9782922145441</th>\n",
       "      <th>9871138016</th>\n",
       "      <th>9995585227</th>\n",
       "      <th>B0000BLD7X</th>\n",
       "      <th>B158991965</th>\n",
       "      <th>N3453124715</th>\n",
       "      <th>O6712345670</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>User-ID</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>276725</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>276726</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>276727</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>276729</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>276733</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 4150 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "ISBN     0002231115  0002232766  0002240114  000225669X  000254794  \\\n",
       "User-ID                                                              \n",
       "276725          0.0         0.0         0.0         0.0        0.0   \n",
       "276726          0.0         0.0         0.0         0.0        0.0   \n",
       "276727          0.0         0.0         0.0         0.0        0.0   \n",
       "276729          0.0         0.0         0.0         0.0        0.0   \n",
       "276733          0.0         0.0         0.0         0.0        0.0   \n",
       "\n",
       "ISBN     0006128831  0006379702  0006485294  000651118X  0006511929  ...  \\\n",
       "User-ID                                                              ...   \n",
       "276725          0.0         0.0         0.0         0.0         0.0  ...   \n",
       "276726          0.0         0.0         0.0         0.0         0.0  ...   \n",
       "276727          0.0         0.0         0.0         0.0         0.0  ...   \n",
       "276729          0.0         0.0         0.0         0.0         0.0  ...   \n",
       "276733          0.0         0.0         0.0         0.0         0.0  ...   \n",
       "\n",
       "ISBN     9727474799  9728608225  9728609187  9782922145441  9871138016  \\\n",
       "User-ID                                                                  \n",
       "276725          0.0         0.0         0.0            0.0         0.0   \n",
       "276726          0.0         0.0         0.0            0.0         0.0   \n",
       "276727          0.0         0.0         0.0            0.0         0.0   \n",
       "276729          0.0         0.0         0.0            0.0         0.0   \n",
       "276733          0.0         0.0         0.0            0.0         0.0   \n",
       "\n",
       "ISBN     9995585227  B0000BLD7X  B158991965  N3453124715  O6712345670  \n",
       "User-ID                                                                \n",
       "276725          0.0         0.0         0.0          0.0          0.0  \n",
       "276726          0.0         0.0         0.0          0.0          0.0  \n",
       "276727          0.0         0.0         0.0          0.0          0.0  \n",
       "276729          0.0         0.0         0.0          0.0          0.0  \n",
       "276733          0.0         0.0         0.0          0.0          0.0  \n",
       "\n",
       "[5 rows x 4150 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rating_matrix.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c829b648",
   "metadata": {},
   "source": [
    "3.计算书籍之间的相似性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c3af52f7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>User-ID</th>\n",
       "      <th>276725</th>\n",
       "      <th>276726</th>\n",
       "      <th>276727</th>\n",
       "      <th>276729</th>\n",
       "      <th>276733</th>\n",
       "      <th>276736</th>\n",
       "      <th>276737</th>\n",
       "      <th>276744</th>\n",
       "      <th>276745</th>\n",
       "      <th>276746</th>\n",
       "      <th>...</th>\n",
       "      <th>278394</th>\n",
       "      <th>278396</th>\n",
       "      <th>278398</th>\n",
       "      <th>278400</th>\n",
       "      <th>278401</th>\n",
       "      <th>278407</th>\n",
       "      <th>278409</th>\n",
       "      <th>278411</th>\n",
       "      <th>278413</th>\n",
       "      <th>278418</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ISBN</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0002231115</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0002232766</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0002240114</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000225669X</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000254794</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9995585227</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B0000BLD7X</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B158991965</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>N3453124715</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>O6712345670</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>4150 rows × 679 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "User-ID      276725  276726  276727  276729  276733  276736  276737  276744  \\\n",
       "ISBN                                                                          \n",
       "0002231115      0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0   \n",
       "0002232766      0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0   \n",
       "0002240114      0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0   \n",
       "000225669X      0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0   \n",
       "000254794       0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0   \n",
       "...             ...     ...     ...     ...     ...     ...     ...     ...   \n",
       "9995585227      0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0   \n",
       "B0000BLD7X      0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0   \n",
       "B158991965      0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0   \n",
       "N3453124715     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0   \n",
       "O6712345670     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0   \n",
       "\n",
       "User-ID      276745  276746  ...  278394  278396  278398  278400  278401  \\\n",
       "ISBN                         ...                                           \n",
       "0002231115      0.0     0.0  ...     0.0     0.0     0.0     0.0     0.0   \n",
       "0002232766      0.0     0.0  ...     0.0     0.0     0.0     0.0     0.0   \n",
       "0002240114      0.0     0.0  ...     0.0     0.0     0.0     0.0     0.0   \n",
       "000225669X      0.0     0.0  ...     0.0     0.0     0.0     0.0     0.0   \n",
       "000254794       0.0     0.0  ...     0.0     0.0     0.0     0.0     0.0   \n",
       "...             ...     ...  ...     ...     ...     ...     ...     ...   \n",
       "9995585227      0.0     0.0  ...     0.0     0.0     0.0     0.0     0.0   \n",
       "B0000BLD7X      0.0     0.0  ...     0.0     0.0     0.0     0.0     0.0   \n",
       "B158991965      0.0     0.0  ...     0.0     0.0     0.0     0.0     0.0   \n",
       "N3453124715     0.0     0.0  ...     0.0     0.0     0.0     0.0     0.0   \n",
       "O6712345670     0.0     0.0  ...     0.0     0.0     0.0     0.0     0.0   \n",
       "\n",
       "User-ID      278407  278409  278411  278413  278418  \n",
       "ISBN                                                 \n",
       "0002231115      0.0     0.0     0.0     0.0     0.0  \n",
       "0002232766      0.0     0.0     0.0     0.0     0.0  \n",
       "0002240114      0.0     0.0     0.0     0.0     0.0  \n",
       "000225669X      0.0     0.0     0.0     0.0     0.0  \n",
       "000254794       0.0     0.0     0.0     0.0     0.0  \n",
       "...             ...     ...     ...     ...     ...  \n",
       "9995585227      0.0     0.0     0.0     0.0     0.0  \n",
       "B0000BLD7X      0.0     0.0     0.0     0.0     0.0  \n",
       "B158991965      0.0     0.0     0.0     0.0     0.0  \n",
       "N3453124715     0.0     0.0     0.0     0.0     0.0  \n",
       "O6712345670     0.0     0.0     0.0     0.0     0.0  \n",
       "\n",
       "[4150 rows x 679 columns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rating_matrix.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "85501650",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算项目相似度矩阵   只是为了查看相似性\n",
    "item_similarity_matrix = cosine_similarity(rating_matrix.T)   #余弦相似性\n",
    "item_similarity_matrix[0:15,0:15]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "caa628a0",
   "metadata": {},
   "source": [
    "4.后续计算评分操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0832f23e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>ISBN</th>\n",
       "      <th>0002231115</th>\n",
       "      <th>0002232766</th>\n",
       "      <th>0002240114</th>\n",
       "      <th>000225669X</th>\n",
       "      <th>000254794</th>\n",
       "      <th>0006128831</th>\n",
       "      <th>0006379702</th>\n",
       "      <th>0006485294</th>\n",
       "      <th>000651118X</th>\n",
       "      <th>0006511929</th>\n",
       "      <th>...</th>\n",
       "      <th>9727474799</th>\n",
       "      <th>9728608225</th>\n",
       "      <th>9728609187</th>\n",
       "      <th>9782922145441</th>\n",
       "      <th>9871138016</th>\n",
       "      <th>9995585227</th>\n",
       "      <th>B0000BLD7X</th>\n",
       "      <th>B158991965</th>\n",
       "      <th>N3453124715</th>\n",
       "      <th>O6712345670</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>User-ID</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>276725</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>276726</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>276727</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>276729</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>276733</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>278407</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>278409</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>278411</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>278413</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>278418</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>679 rows × 4150 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "ISBN     0002231115  0002232766  0002240114  000225669X  000254794  \\\n",
       "User-ID                                                              \n",
       "276725          0.0         0.0         0.0         0.0        0.0   \n",
       "276726          0.0         0.0         0.0         0.0        0.0   \n",
       "276727          0.0         0.0         0.0         0.0        0.0   \n",
       "276729          0.0         0.0         0.0         0.0        0.0   \n",
       "276733          0.0         0.0         0.0         0.0        0.0   \n",
       "...             ...         ...         ...         ...        ...   \n",
       "278407          0.0         0.0         0.0         0.0        0.0   \n",
       "278409          0.0         0.0         0.0         0.0        0.0   \n",
       "278411          0.0         0.0         0.0         0.0        0.0   \n",
       "278413          0.0         0.0         0.0         0.0        0.0   \n",
       "278418          0.0         0.0         0.0         0.0        0.0   \n",
       "\n",
       "ISBN     0006128831  0006379702  0006485294  000651118X  0006511929  ...  \\\n",
       "User-ID                                                              ...   \n",
       "276725          0.0         0.0         0.0         0.0         0.0  ...   \n",
       "276726          0.0         0.0         0.0         0.0         0.0  ...   \n",
       "276727          0.0         0.0         0.0         0.0         0.0  ...   \n",
       "276729          0.0         0.0         0.0         0.0         0.0  ...   \n",
       "276733          0.0         0.0         0.0         0.0         0.0  ...   \n",
       "...             ...         ...         ...         ...         ...  ...   \n",
       "278407          0.0         0.0         0.0         0.0         0.0  ...   \n",
       "278409          0.0         0.0         0.0         0.0         0.0  ...   \n",
       "278411          0.0         0.0         0.0         0.0         0.0  ...   \n",
       "278413          0.0         0.0         0.0         0.0         0.0  ...   \n",
       "278418          0.0         0.0         0.0         0.0         0.0  ...   \n",
       "\n",
       "ISBN     9727474799  9728608225  9728609187  9782922145441  9871138016  \\\n",
       "User-ID                                                                  \n",
       "276725          0.0         0.0         0.0            0.0         0.0   \n",
       "276726          0.0         0.0         0.0            0.0         0.0   \n",
       "276727          0.0         0.0         0.0            0.0         0.0   \n",
       "276729          0.0         0.0         0.0            0.0         0.0   \n",
       "276733          0.0         0.0         0.0            0.0         0.0   \n",
       "...             ...         ...         ...            ...         ...   \n",
       "278407          0.0         0.0         0.0            0.0         0.0   \n",
       "278409          0.0         0.0         0.0            0.0         0.0   \n",
       "278411          0.0         0.0         0.0            0.0         0.0   \n",
       "278413          0.0         0.0         0.0            0.0         0.0   \n",
       "278418          0.0         0.0         0.0            0.0         0.0   \n",
       "\n",
       "ISBN     9995585227  B0000BLD7X  B158991965  N3453124715  O6712345670  \n",
       "User-ID                                                                \n",
       "276725          0.0         0.0         0.0          0.0          0.0  \n",
       "276726          0.0         0.0         0.0          0.0          0.0  \n",
       "276727          0.0         0.0         0.0          0.0          0.0  \n",
       "276729          0.0         0.0         0.0          0.0          0.0  \n",
       "276733          0.0         0.0         0.0          0.0          0.0  \n",
       "...             ...         ...         ...          ...          ...  \n",
       "278407          0.0         0.0         0.0          0.0          0.0  \n",
       "278409          0.0         0.0         0.0          0.0          0.0  \n",
       "278411          0.0         0.0         0.0          0.0          0.0  \n",
       "278413          0.0         0.0         0.0          0.0          0.0  \n",
       "278418          0.0         0.0         0.0          0.0          0.0  \n",
       "\n",
       "[679 rows x 4150 columns]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将rating_matrix的索引转换为字符串    \n",
    "rating_matrix.index = rating_matrix.index.map(str)    #str为一个函数的参数  ，提高容错\n",
    "rating_matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "90e0658b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['276725', '276726', '276727', '276729', '276733', '276736', '276737',\n",
       "       '276744', '276745', '276746',\n",
       "       ...\n",
       "       '278394', '278396', '278398', '278400', '278401', '278407', '278409',\n",
       "       '278411', '278413', '278418'],\n",
       "      dtype='object', name='User-ID', length=679)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rating_matrix.index"
   ]
  },
  {
   "cell_type": "raw",
   "id": "2d300fb5",
   "metadata": {},
   "source": [
    "user = '276729'\n",
    "item = '000225669X'\n",
    "题目：未来要预测这个user对这个书籍item的评分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "42e917f8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "User-ID\n",
       "276725    0.0\n",
       "276726    0.0\n",
       "276727    0.0\n",
       "276729    0.0\n",
       "276733    0.0\n",
       "         ... \n",
       "278407    0.0\n",
       "278409    0.0\n",
       "278411    0.0\n",
       "278413    0.0\n",
       "278418    0.0\n",
       "Name: 000225669X, Length: 679, dtype: float64"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user = '276729'\n",
    "rating_matrix['000225669X']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "69e0c7c5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['0002231115', '0002232766', '0002240114', '000225669X', '000254794',\n",
       "       '0006128831', '0006379702', '0006485294', '000651118X', '0006511929',\n",
       "       ...\n",
       "       '9727474799', '9728608225', '9728609187', '9782922145441', '9871138016',\n",
       "       '9995585227', 'B0000BLD7X', 'B158991965', 'N3453124715', 'O6712345670'],\n",
       "      dtype='object', name='ISBN', length=4150)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rating_matrix.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "d8c9553e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "item = '000225669X'\n",
    "# 获取item的索引,查看这本书在第几列，也就是item索引\n",
    "item_index = rating_matrix.columns.get_loc(item)  #这本书在这个df中的多少行    用于后续获取书籍的相似性\n",
    "item_index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "02c3876b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 0., 0., ..., 0., 0., 0.])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取item的相似度分数\n",
    "similarity_scores = item_similarity_matrix[item_index]\n",
    "similarity_scores"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "ae6aac63",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([  3, 338], dtype=int64),)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.where(similarity_scores>0)    #看有没有相似性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "80320087",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ISBN\n",
       "0002231115     0.0\n",
       "0002232766     0.0\n",
       "0002240114     0.0\n",
       "000225669X     0.0\n",
       "000254794      0.0\n",
       "              ... \n",
       "9995585227     0.0\n",
       "B0000BLD7X     0.0\n",
       "B158991965     0.0\n",
       "N3453124715    0.0\n",
       "O6712345670    0.0\n",
       "Name: 276729, Length: 4150, dtype: float64"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取用户评分\n",
    "user_ratings = rating_matrix.loc[user]\n",
    "user_ratings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "38ed5844",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['0002231115', '0002232766', '0002240114', '000225669X', '000254794',\n",
       "       '0006128831', '0006379702', '0006485294', '000651118X', '0006511929',\n",
       "       ...\n",
       "       '9727474799', '9728608225', '9728609187', '9782922145441', '9871138016',\n",
       "       '9995585227', 'B0000BLD7X', 'B158991965', 'N3453124715', 'O6712345670'],\n",
       "      dtype='object', name='ISBN', length=4150)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_ratings.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "2c3a7c5b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['052165615X', '0521795028'], dtype='object', name='ISBN')"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取用户评分过的物品\n",
    "rated_items = user_ratings[user_ratings>0].index\n",
    "rated_items"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "86098c58",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 在用户评分过的物品中获取与item相似的物品\n",
    "similar_items = [item for item in rated_items if item_similarity_matrix[item_index][rating_matrix.columns.get_loc(item)]> 0]\n",
    "similar_items"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "d92bb403",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rating_matrix.columns.get_loc(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "b34f1726",
   "metadata": {},
   "outputs": [],
   "source": [
    "#similar_item为空列表，说明用户评过分的书籍中没有和item相似的书籍，如果是非空列表的情况下，那么将继续如下步骤：\n",
    "# 这里把上面的similar_items造一个非空的出来\n",
    "similar_items = ['052165615X','0521795028']\n",
    "similar_item_index = []   # 保存评分的\n",
    "for i in range(len(similar_items)):\n",
    "    similar_item_index.append(rating_matrix.columns.get_loc(similar_items[i]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "8c33ca04",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1903, 1904]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "similar_item_index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "4a3ed99e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ISBN\n",
       "0002231115    0.0\n",
       "0002232766    0.0\n",
       "Name: 276729, dtype: float64"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_ratings[['0002231115','0002232766']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "6994223f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_ratings['0002231115']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "c3035ece",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4.5\n"
     ]
    }
   ],
   "source": [
    "# 如果没有相似物品，则返回0\n",
    "if len(similar_items) == 0:\n",
    "    print(0)\n",
    "# 否则，计算用户对item的预测评分\n",
    "else:\n",
    "#     print(user_ratings[similar_items].dot(similarity_scores[similar_item_index])/similarity_scores[similar_item_index].sum())  #求均值\n",
    "# 也可以这样\n",
    "    print(user_ratings[similar_items].mean())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
